(function() {
base2Cells.util.namespace("base2Cells.ytPlaylist");

var Global = base2Cells.ytPlaylist.Global;

var ns = base2Cells.ytplaylist;
base2Cells.ytPlaylist.AddWindow = {
    show: function() {
        this.window.style.top = "40px";
    },

    hide: function() {
        this.window.style.top = "-400px";
    },

    init: function(elId) {
        var el = document.getElementById(elId);

        this.window = document.createElement('div');
        this.artistLabel = document.createElement('div');
        this.artistInput = document.createElement('input');
        this.titleLabel = document.createElement('div');
        this.titleInput = document.createElement('input');
        this.urlLabel = document.createElement('div');
        this.urlInput = document.createElement('input');
        this.submitButton = document.createElement('input');
        this.cancelButton = document.createElement('input');

        this.window.setAttribute('class', 'addWindow');
        this.artistLabel.setAttribute('class', 'artistLabel');
        this.artistInput.setAttribute('class', 'artistInput');
        this.titleLabel.setAttribute('class', 'titleLabel');
        this.titleInput.setAttribute('class', 'titleInput');
        this.urlLabel.setAttribute('class', 'urlLabel');
        this.urlInput.setAttribute('class', 'urlInput');
        this.submitButton.setAttribute('class', 'submitButton');
        this.cancelButton.setAttribute('class', 'cancelButton');

        this.artistLabel.setAttribute('id', 'artistLabel-id');
        this.artistInput.setAttribute('id', 'artistInput-id');
        this.titleLabel.setAttribute('id', 'titleLabel-id');
        this.titleInput.setAttribute('id', 'titleInput-id');
        this.urlLabel.setAttribute('id', 'urlLabel-id');
        this.urlInput.setAttribute('id', 'urlInput-id');
        this.submitButton.setAttribute('id', 'submitButton-id');
        this.cancelButton.setAttribute('id', 'cancelButton-id');

        this.artistLabel.innerHTML = 'Artist:';
        this.titleLabel.innerHTML = 'Title:';
        this.urlLabel.innerHTML = 'URL:';

        this.artistInput.setAttribute('type', 'text');
        this.titleInput.setAttribute('type', 'text');
        this.urlInput.setAttribute('type', 'text');
        this.submitButton.setAttribute('type', 'button');
        this.cancelButton.setAttribute('type', 'button');
        this.submitButton.setAttribute('value', 'Add');
        this.cancelButton.setAttribute('value', 'Cancel');

        var scope = this;

        this.cancelButton.onclick = cancelFunction;
        function cancelFunction() {
            scope.artistInput.value = "";
            scope.titleInput.value = "";
            scope.urlInput.value = "";

            base2Cells.ytPlaylist.AddWindow.hide();
        }

        this.submitButton.onclick = submitFunction;
        function submitFunction() {
            var artist = scope.artistInput.value;
            var title = scope.titleInput.value;
            var url = scope.urlInput.value;

            scope.artistInput.value = "";
            scope.titleInput.value = "";
            scope.urlInput.value = "";

            Global.itemContainer.addSong({
                artist: artist,
                title: title,
                ytUrl: url
            });

            Global.iAddedSong = true;

            if (wave.isInWaveContainer()) {
                var state = wave.getState();
                state && state.submitValue('listChange', wave.util.printJson({
                    artist: artist,
                    title: title,
                    ytUrl: url,
                    add: true
                }));
            }

            base2Cells.ytPlaylist.AddWindow.hide();
        }
//        var currentKeyPress = document.onkeypress;
//        document.onkeypress = function(cfg) {
//            currentKeyPress && currentKeyPress();
//
//            // If enter is pressed
//            if (cfg.keyCode == 13) {
//                submitFunction();
//            } else if (cfg.keyCode == 27) { // If esc is pressed
//                cancelFunction();
//            }
//        };

        this.window.appendChild(this.artistLabel);
        this.window.appendChild(this.artistInput);
        this.window.appendChild(this.titleLabel);
        this.window.appendChild(this.titleInput);
        this.window.appendChild(this.urlLabel);
        this.window.appendChild(this.urlInput);
        this.window.appendChild(this.submitButton);
        this.window.appendChild(this.cancelButton);

        el.appendChild(this.window);
    }
}
})();
